/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.control.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.risk.control.dto.RiskUnitEventDTO;
import com.hopes.bizz.risk.control.dto.RiskUnitEventExcelDTO;
import com.hopes.bizz.risk.control.entity.RiskUnitEventEntity;
import com.hopes.bizz.risk.control.service.RiskUnitEventService;
import com.hopes.bizz.risk.control.vo.RiskUnitEventExcelVO;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.Assert;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 风险事件
 *
 * @author pig
 * @date 2023-07-19 16:17:44
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskUnitEvent" )
@Tag(description = "event" , name = "风险事件管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskUnitEventController {

    private final RiskUnitEventService riskUnitEventService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param riskUnitEvent 风险事件
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('risk_unit_event_view')" )
    public R getRiskUnitEventPage(Page page, RiskUnitEventDTO riskUnitEvent) {
        return R.ok(riskUnitEventService.page(page, riskUnitEvent));
    }

    /**
     * 通过id查询风险事件
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('risk_unit_event_view')" )
    public R getById(@PathVariable("id" ) String id) {
        return R.ok(riskUnitEventService.getById(id));
    }

    /**
     * 新增风险事件
     * @param riskUnitEventDTO 风险事件
     * @return R
     */
    @Operation(summary = "新增风险事件" , description = "新增风险事件" )
    @SysLog("新增风险事件" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('risk_unit_event_add')" )
    @CacheEvict(value = DictResolver.CACHE_NAME_DICT, allEntries = true)
    public R save(@RequestBody RiskUnitEventDTO riskUnitEventDTO) {
        return R.ok(riskUnitEventService.save(riskUnitEventDTO));
    }

    /**
     * 修改风险事件
     * @param riskUnitEventDTO 风险事件
     * @return R
     */
    @Operation(summary = "修改风险事件" , description = "修改风险事件" )
    @SysLog("修改风险事件" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('risk_unit_event_edit')" )
    @CacheEvict(value = DictResolver.CACHE_NAME_DICT, allEntries = true)
    public R updateById(@RequestBody RiskUnitEventDTO riskUnitEventDTO) {
        Assert.notNull(riskUnitEventDTO.getId(), "风险事件Id不可为空");
        return R.ok(riskUnitEventService.updateById(riskUnitEventDTO));
    }

    /**
     * 批量更新管控措施
     * @param riskUnitEventDTO 风险事件（管控措施列表）
     * @return R
     */
    @Operation(summary = "批量更新管控措施" , description = "批量更新管控措施" )
    @SysLog("批量更新管控措施" )
    @PutMapping("/updateControlList")
    @PreAuthorize("@pms.hasPermission('risk_unit_event_edit')" )
    @CacheEvict(value = DictResolver.CACHE_NAME_DICT, allEntries = true)
    public R updateControlListById(@RequestBody RiskUnitEventDTO riskUnitEventDTO) {
        Assert.notNull(riskUnitEventDTO.getId(), "风险事件Id不可为空");
        Assert.isTrue(CollectionUtil.isNotEmpty(riskUnitEventDTO.getRiskControlMeasureList()), "管控措施不可为空");
        return R.ok(riskUnitEventService.updateControlListById(riskUnitEventDTO));
    }

    /**
     * 通过id删除风险事件
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除风险事件" , description = "通过id删除风险事件" )
    @SysLog("通过id删除风险事件" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('risk_unit_event_del')" )
    @CacheEvict(value = DictResolver.CACHE_NAME_DICT, allEntries = true)
    public R removeById(@RequestBody String[] ids) {
        return R.ok(riskUnitEventService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('risk_unit_event_export')")
    public List<RiskUnitEventExcelVO> template() {
        return riskUnitEventService.exportExcel(Collections.singletonList(new RiskUnitEventEntity()));
    }

    /**
     * 导出excel 表格
     * @param riskUnitEventExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('risk_unit_event_export')")
    public List<RiskUnitEventExcelVO> export(RiskUnitEventExcelDTO riskUnitEventExcelDto) {
        return riskUnitEventService.exportExcel(riskUnitEventService.list());
    }

    /**
     * 导入excel 表格
     * @param riskUnitEventExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('risk_unit_event_export')")
    @CacheEvict(value = DictResolver.CACHE_NAME_DICT, allEntries = true)
    public R importExcel(@RequestExcel List<RiskUnitEventExcelDTO> riskUnitEventExcelDtoList, BindingResult bindingResult) {
        return riskUnitEventService.importExcel(riskUnitEventExcelDtoList, bindingResult);
    }
}
